import { user } from "@/api";
import { defineStore } from "pinia"

export const useUserStore = defineStore("user", {
    state(): { menus: IUserInfoMenus[], username: string, tableData: IuserList[], search: IAdminListreq, total: number } {
        return {
            menus: [],
            username: "",
            tableData: [],
            search: {
                keyword: "",
                pageNum: 1,
                pageSize: 10,
            },
            total: 0,
        }
    },
    getters: {
        activeMenus() {
            let arr: IActive[] = this.menus.filter(item => item.parentId == 0)
            console.log(arr);
            this.menus.forEach(p => {
                const child = arr.find(c => c.id == p.parentId && p.hidden == 0)
                console.log(child);
                if (child) {
                    if (!child.children) {
                        child.children = []
                    }
                    child.children.push(p)
                }
            })
            return arr
        }
    },
    actions: {
        listFn() {
            user.getUserListApi(this.search).then(res => {
                if (res.code == 200) {
                    this.tableData = res.data.list;
                    this.total = res.data.total
                }
            })
        }

    }

})